import 'package:flutter/material.dart';

const _fullLengthCode = 65248;

/// 防止文字自动换行
extension FixAutoLines on String {
  String fixAutoLines() {
    // return Characters(this).join('\u{200B}');
    return Characters(this).join('\u2060');
  }

  String alphanumericToFullLength() {
    final regex = RegExp(r'^[a-zA-Z0-9]+$');
    final string = runes.map<String>((rune) {
      final char = String.fromCharCode(rune);
      return regex.hasMatch(char)
          ? String.fromCharCode(rune + _fullLengthCode)
          : char;
    });
    return string.join();
  }

  String alphanumericToHalfLength() {
    final regex = RegExp(r'^[Ａ-Ｚａ-ｚ０-９]+$');
    final string = runes.map<String>((rune) {
      final char = String.fromCharCode(rune);
      return regex.hasMatch(char)
          ? String.fromCharCode(rune - _fullLengthCode)
          : char;
    });
    return string.join();
  }

  String safeSubstring(int start, [int? end]) {
    List<int> runeList = runes.toList();

    end ??= runeList.length;
    if (end > runeList.length) {
      end = runeList.length;
    }
    return String.fromCharCodes(runeList.sublist(start, end));
  }
}
